#!/usr/bin/env python
# encoding: utf-8
'''
@author: wangjianrong
@software: pycharm
@file: ccoordinate_transforms.py
@time: 2020/9/28 9:10
@desc:
'''
import numpy as np

def xyxy2xywh(bboxes):
    '''
    :param bboxes:Nx4 np.ndarray xmin,ymin,xmax,ymax
    :return: xc yc width height
    '''
    if not isinstance(bboxes,np.ndarray):
        bboxes = np.array(bboxes)
    if len(bboxes.shape)==1:
        bboxes = bboxes[None,:]
    new_bboxes = np.zeros_like(bboxes)
    new_bboxes[:,0] = (bboxes[:,0] + bboxes[:,2]) / 2
    new_bboxes[:,1] = (bboxes[:,1] + bboxes[:,3]) / 2
    new_bboxes[:,2] = bboxes[2] - bboxes[:,0]
    new_bboxes[:,3] = bboxes[3] - bboxes[:,1]
    return new_bboxes
